package com.woniuxy.carlife.controller;


import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.domain.ServiceAppraise;
import com.woniuxy.carlife.dto.AllAppraiseWithId;
import com.woniuxy.carlife.param.AppraiseParam;
import com.woniuxy.carlife.param.StoreOrServiceAppraiseParam;
import com.woniuxy.carlife.service.ServiceAppraiseService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author fx
 * @ClassName:ServiceAppraiseController
 * @Description: 门店服务评价控制类
 * @date 2021/4/6 20:00
 **/

@RestController
@RequestMapping("/serviceAppraise")
@Slf4j
@Api(tags = "服务评论接口信息")
public class ServiceAppraiseController {

    @Resource
    private ServiceAppraiseService service;

    /**
     * 功能描述〈用户查询自己所有的评价,同时查询对应门店和对应的服务基本信息〉
     * @author:fx
     * @param: Integer userId: 用户id
     * @return: Result
     * @Date 2021/4/6 23:18
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "用户查询自己所有的评价",notes = "<span style='color:red;'>用户查询自己所有的评价,同时查询对应门店和对应的服务基本信息</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功",response = AllAppraiseWithId.class),
            @ApiResponse(code=20001,message = "查询失败"),
            @ApiResponse(code=30001,message = "参数错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("/ordinary/userallappraise")
    public Result findUserAllAppraiseByUserId(Integer userId){
        log.info("findUserAllAppraiseByUserId开始:参数：{}",userId);
        //判断参数是否有效
        if (userId==null||userId<=0){
            log.info("findUserAllAppraiseByUserId结束:返回:{}","输入参数为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"输入参数错误");
        }
        List<AllAppraiseWithId> appraises = service.findUserOwnAppraiseByUserId(userId);
        if (ObjectUtils.isEmpty(appraises)){
            log.info("findUserAllAppraiseByUserId结束:返回:{}","查询失败，没找到数据");
            return new Result(false, StatusCode.NULLDATA,"查询失败，没查到数据");
        }
        log.info("findUserAllAppraiseByUserId结束:返回:{}",appraises);
        return new Result(true, StatusCode.OK,"查询成功",appraises);
    }

    /**
     * 功能描述〈根据门店id查询当前门店对应的评价信息〉
     * @author:fx
     * @param: @RequestBody StoreOrServiceAppraiseParam appraise: 门店id
     * @return:
     * @Date 2021/4/6 23:26
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据门店id查询当前门店对应的评价信息",notes = "<span style='color:red;'>根据门店id查询当前门店对应的评价信息</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功",response = AllAppraiseWithId.class),
            @ApiResponse(code=20001,message = "查询失败"),
            @ApiResponse(code=00000,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId",value = "门店id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("allstoreappraise")
    public Result findStoreAllAppraiseByStoreId(Integer storeId){
        log.info("findStoreAllAppraiseByStoreId开始:参数：{}",storeId);
        //判断参数是否有效
        if (storeId==null||storeId<=0){
            log.info("findStoreAllAppraiseByStoreId结束:返回:{}","输入参数为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"输入参数错误");
        }
        List<AllAppraiseWithId> storeAppraiseByStoreId = service.findStoreAppraiseByStoreId(storeId);
        if (ObjectUtils.isEmpty(storeAppraiseByStoreId)){
            log.info("findStoreAllAppraiseByStoreId结束:返回：{}","没有查询到数据");
            return new Result(false,StatusCode.NULLDATA,"没有查询到数据");
        }
        log.info("findStoreAllAppraiseByStoreId结束:返回：{}",storeAppraiseByStoreId);
        return new Result(true,StatusCode.OK,"成功查询到数据",storeAppraiseByStoreId);
    }

    /**
     * 功能描述〈根据评论id查询对应评论详情〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/9 23:37
     * @throws
     */
    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "根据评论id查询对应评论详情",notes = "<span style='color:red;'>根据评论id查询对应评论详情</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "查询成功",response = ServiceAppraise.class),
            @ApiResponse(code=20001,message = "查询失败"),
            @ApiResponse(code=00000,message = "没有数据")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "appraiseId",value = "评价id",dataType = "Integer",paramType = "query",example = "1"),
    })
    @GetMapping("serviceappraise")
    public Result findServiceAppraiseInfoByServiceAppraiseId(Integer appraiseId){
        log.info("findServiceAppraiseInfoByServiceAppraiseId开始:参数：{}",appraiseId);
        //判断参数是否有效
        if (appraiseId==null||appraiseId<=0){
            log.info("findServiceAppraiseInfoByServiceAppraiseId结束:返回:{}","输入参数为空或者小于1");
            return new Result(false, StatusCode.PARAMERROR,"输入参数错误");
        }
        ServiceAppraise appraiseInfoByAppraiseId =
                service.findAppraiseInfoByAppraiseId(appraiseId);
        //判断是否有数据
        if (ObjectUtils.isEmpty(appraiseInfoByAppraiseId.getServiceAppraiseInfo())){
            log.info("findServiceAppraiseInfoByServiceAppraiseId结束:返回：{}","没有查询到数据");
            return new Result(false,StatusCode.ERROR,"没有查询到数据");
        }
        log.info("findServiceAppraiseInfoByServiceAppraiseId结束:返回：{}","appraiseInfoByAppraiseId");
        return new Result(true,StatusCode.OK,"成功查询到数据",appraiseInfoByAppraiseId);
    }

    /**
     * 功能描述〈用户添加对指定服务的评论〉
     * @author:fx
     * @param: @RequestBody AppraiseParam appraiseParam: 提交对服务评价的VO类，包括图片上传
     * @return: Result
     * @Date 2021/4/7 11:14
     * @throws 
     */
    @ApiOperation(value = "用户添加对指定服务的评论",notes = "<span style='color:red;'>用户添加对指定服务的评论</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "评论成功"),
            @ApiResponse(code=20001,message = "评论失败"),
            @ApiResponse(code=30001,message = "参数错误"),
            @ApiResponse(code=30002,message = "数据已存在")

    })
    @PostMapping("/ordinary/userappraise")
    public Result saveAppraise(@RequestBody @ApiParam AppraiseParam appraiseParam) throws IOException {
        log.info("saveAppraise开始:参数：{}",appraiseParam);
        //判断传入参数的情况
        if (appraiseParam.getServiceAppraiseInfo()==null||
                appraiseParam.getServiceAppraiseInfo().trim().length()==0||
                appraiseParam.getServiceId()==null||
                appraiseParam.getServiceId()<=0||
                appraiseParam.getUserId()==null||
                appraiseParam.getUserId()<=0||
                appraiseParam.getStoreId()==null||
                appraiseParam.getStoreId()<=0||
                appraiseParam.getOrderId()==null||
                appraiseParam.getOrderId()<=0){
            log.info("saveAppraise结束:查询失败:{}","参数错误");
            return new Result(false,StatusCode.PARAMERROR,"参数错误");
        }
        int i = service.saveAppraiseInfoToService(appraiseParam);
        if (i==1){
            log.info("saveAppraise结束:结果：{}","评论成功");
            return new Result(true,StatusCode.OK,"评论成功");
        }else if (i==0){
            log.info("saveAppraise结束:结果：{}","评论失败");
            return new Result(false,StatusCode.ERROR,"评论失败");
        }else if (i==-1){
            log.info("saveAppraise结束:结果：{}","评论失败,该用户已经对该订单的指定服务评论");
            return new Result(false,StatusCode.DATAISHERE,"已经评论过");
        }
        //这个返回只是为以后做准备，返回情况不存在这一项
        return null;
    }

    /**
     * 功能描述〈评论图片上传〉
     * @author:fx
     * @param: @param null:
     * @return:
     * @Date 2021/4/13 2:13
     * @throws
     */
    @ApiOperation(value = "评论图片上传",notes = "<span style='color:red;'>评论图片上传，集合类型，单张图片大小限制为1m</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 20000,message = "图片上传成功"),
            @ApiResponse(code=20001,message = "图片上传失败")
    })
    @PostMapping("/ordinary/appraisepic")
    public String uploadAppraisePic(@ModelAttribute @ApiParam List<MultipartFile> pics) throws IOException {
        log.info("uploadAppraisePic开始:参数：{}",pics);
        String s = service.savaApprasiePic(pics);
        if (s!=null){
            log.info("uploadAppraisePic结束:返回：{}",s);
            return s;
        }
        log.info("uploadAppraisePic结束:返回：{}","图片上传失败");
        return "图片上传失败";
    }
}

